/**
 * Created by python on 19-8-26.
 */
/**
 * Created by python on 19-8-25.
 //  */
var app = new Vue({
    el: "#app",
    data: {
        // 页面中需要使用到的数据，键值对
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        question_author: '',
        title: '',
        detail: '',
        answers: [],
        count: 0,
        comment_num: 0,
        tags: null,
        create_time: '',
        update_time: '',
        question_id: 0,
        is_click: null,
        author_id: null,
        answer_id: 0,
        question_comment: false,
        adetail: '',
        equestion: '',
        question_edit: false,


    },
    mounted: function () {
        this.question_id = this.get_query_string("id");
        console.log(this.question_id)


        axios.get(HOST + 'question/' + this.question_id + '/'
            // , {
            // headers: {
            //     'Authorization': 'JWT ' + this.token
            // },}
            )

            .then(response => {
                this.question_author = response.data.author;
                this.title = response.data.title;
                this.detail = response.data.detail;
                this.count = response.data.count;
                this.comment_num = response.data.comment_num;
                this.tags = response.data.tag;
                this.create_time = response.data.create_time;
                this.update_time = response.data.update_time;
                this.answers = response.data.answers.reverse();
                this.is_click = response.data.is_click;
                // this.question_id=response.data.id;
                console.log(this.question_id);
                console.log(this.user_id)

            })


        // 一加载就需要做的，直接是代码
    },
    methods: {

        // 从路径中提取sku_id
        get_query_string: function (name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        },
        // 添加和取消关注
        change_focus: function () {
            axios.put(HOST + 'question/focus/', {

                user: this.user_id,
                question: this.question_id,
            }

            )
                .then(function (response) {
                    console.log("成功");
                    location.reload()
                })
                .catch(function (error) {
                    console.log(error);
                });
        },
        // 点赞和取消点赞
        userful_add: function () {
            axios.put(HOST + 'question/useful/', {
                user: this.user_id,
                question: this.question_id,
                action: "add",
            })
                .then(function (response) {
                    console.log("成功");
                    console.log(response)
                    location.reload()
                })
                .catch(function (error) {
                    alert("已经赞过啦")
                });

        },
        userful_reduce: function () {
            axios.put(HOST + 'question/useful/', {
                user: this.user_id,
                question: this.question_id,
                action: "reduce",
            })
                .then(function (response) {
                    console.log("成功");
                    console.log(response)
                    location.reload()
                })
                .catch(function (error) {
                    alert("已经踩过啦")
                });

        },
        get_id: function (id) {
            this.answer_id = id
            console.log(this.answer_id)

        },
        // 点赞和取消点赞
        answeruse_add: function (id) {
            axios.put(HOST + 'answer/useful/', {
                user: this.user_id,
                answer: id,
                action: "add",
            })
                .then(function (response) {
                    console.log("成功");
                    console.log(response)
                    location.reload()
                })
                .catch(function (error) {
                    alert("已经赞过啦")
                });

        },
        answeruse_reduce: function (id) {
            axios.put(HOST + 'answer/useful/', {
                user: this.user_id,
                answer: id,
                action: "reduce",
            })
                .then(function (response) {
                    console.log("成功");
                    console.log(response)
                    location.reload()
                })
                .catch(function (error) {
                    alert("已经踩过啦")
                });

        },
        pinglun: function () {
            if (this.question_comment == false && this.question_edit == false) {

                this.question_comment = true
            }
            else if (this.question_comment == false && this.question_edit == true) {
                this.question_comment = true
                this.question_edit = false
            }
            else if (this.question_comment == true && this.question_edit == false) {
                this.question_comment = false
            }


        },
        bianji: function () {
            if (this.question_comment == false && this.question_edit == false) {
                this.question_edit = true
            }
            else if (this.question_comment == true && this.question_edit == false) {
                this.question_comment = false
                this.question_edit = true
            }
            else if (this.question_comment == false && this.question_edit == true) {
                this.question_edit = false
            }
             console.log(this.question_comment)
            console.log(this.question_edit)

        },



        answer: function () {
            console.log(this.adetail);
            axios.post(HOST + "question/answer/" + this.question_id + "/", {
                 headers: {
                    'Authorization': "JWT " + this.token
                },
                author: this.user_id,
                question: this.question_id,
                detail: this.adetail
            })
                .then(function (response) {
                    console.log("回答发表成功");
                    console.log(response)
                    location.reload()
                })
                .catch(function (error) {
                    alert("回答发表失败")
                });

        },
        answer_change: function (id) {
            console.log(id)
            axios.put(HOST + "question/answer/" + id + "/", {
                detail: this.cadetail
            })
                .then(function (response) {
                    console.log("回答更改成功");
                    console.log(response)
                    location.reload()
                })
                .catch(function (error) {
                    alert("回答更改失败")
                });

        },
        question_change: function () {
            console.log(this.question_id)
            console.log(this.equestion)
            axios.put(HOST + "question/change/" + this.question_id + "/", {
                headers: {
                    'Authorization': "JWT " + this.token
                },
                detail:this.equestion

            })
                .then(function (response) {
                    console.log("问题更改成功");
                    console.log(response)
                    location.reload()
                })
                .catch(function (error) {
                    alert("问题更改失败")
                });


        },


    }
})
